package com.iemes.controller.hr;
/**
 * 考勤记录
 */
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.CheckingFormMap;
import com.iemes.entity.StaffFormMap;
import com.iemes.mapper.hr.CheckingMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
@Controller
@RequestMapping("/hrchecking/")
public class CheckingController extends BaseController{
   
	@Inject
	private CheckingMapper checkingMapper;

	//考勤
		@RequestMapping("checkingAttend")
		public String examineOrder(Model model){
            
			model.addAttribute("res", findByRes());
			return Common.BACKGROUND_PATH + "/hr/checking_list";
		}
		//考勤分页
		@ResponseBody
		@RequestMapping("checking_findByPage")
		public PageView findByPage(String pageNow, String pageSize) throws Exception 
		{   

              CheckingFormMap checkingFormMap=getFormMap(CheckingFormMap.class);
              List<Map<String,Object>> checkingFormMaps=checkingMapper.getAllchecking(checkingFormMap);
              LuogicPaging(pageNow, pageSize, checkingFormMaps);
			pageView.setRecords(checkingFormMaps);
			return pageView;
		}

		
		//考勤详情页面
		@RequestMapping("checking_particulars")
		public String examineOrders(Model model){
            String id=getPara("id");
            CheckingFormMap checkingFormMap=checkingMapper.findbyFrist("id", id, CheckingFormMap.class);
            String date=checkingFormMap.getStr("morn_work_time");
            String peopleId=checkingFormMap.get("person_id").toString();
            StaffFormMap StaffFormMaps=checkingMapper.findbyFrist("id", peopleId, StaffFormMap.class);
            //获得日期
            String datess=date.substring(0,date.indexOf(" "));
           model.addAttribute("staff",StaffFormMaps);
			model.addAttribute("check",checkingFormMap);
			model.addAttribute("checkdate",datess);
			
			return Common.BACKGROUND_PATH + "/hr/checking_particulars";
		}

		//编辑考勤页面
		@RequestMapping("checking_edit")
		public String editUI(Model model) throws Exception 
		{
	   String id=getPara("id");
        CheckingFormMap checkingFormMap=checkingMapper.findbyFrist("id", id, CheckingFormMap.class);
        String date=checkingFormMap.getStr("morn_work_time");
        String peopleId=checkingFormMap.get("person_id").toString();
        StaffFormMap StaffFormMaps=checkingMapper.findbyFrist("id", peopleId, StaffFormMap.class);
        //获得日期
        String datess=date.substring(0,date.indexOf(" "));
       model.addAttribute("staff",StaffFormMaps);
		model.addAttribute("check",checkingFormMap);
		model.addAttribute("checkdate",datess);
				return Common.BACKGROUND_PATH + "/hr/cheking_edit";
			}

			
		
		//编辑考勤操作
		@ResponseBody
		@RequestMapping(value="checking_editEntity")
		@SystemLog(module="考勤数据维护",methods="查看考勤-修改考勤")//凡需要处理业务逻辑的.都需要记录操作日志
		public String editEntity() {
			String datetimea=getPara("datetimea");
			String morn_work_timesq=getPara("checkingFormMap.morn_work_time");
			String morn_leave_timesq=getPara("checkingFormMap.morn_leave_time");
			String after_work_timesq=getPara("checkingFormMap.after_work_time");
			String after_leave_timesq=getPara("checkingFormMap.after_leave_time");
			//拼接时间
			String morn_work_timea=datetimea+" "+morn_work_timesq;
			String morn_leave_timea=datetimea+" "+morn_leave_timesq;
			String after_work_timea=datetimea+" "+after_work_timesq;
			String after_leave_timea=datetimea+" "+after_leave_timesq;
			CheckingFormMap checkingFormMap=getFormMap(CheckingFormMap.class);
			checkingFormMap.put("morn_work_time",morn_work_timea );
			checkingFormMap.put("morn_leave_time", morn_leave_timea);
			checkingFormMap.put("after_work_time", after_work_timea);
			checkingFormMap.put("after_leave_time",after_leave_timea );
			SimpleDateFormat dfs = new SimpleDateFormat("HH:mm");
			
			try {
				//上班迟到
				 long mornjudges = dfs.parse("07:40").getTime();
				 long mornjudge = dfs.parse("19:40").getTime();
				//下班早退
					 long forenoon = dfs.parse("11:40").getTime();
					 long forenoons = dfs.parse("07:40").getTime();
				//上班迟到
						long mornjudgec = dfs.parse("13:30").getTime();
						long mornjudgecs = dfs.parse("19:40").getTime();
				//下班早退
						long aftera = dfs.parse("19:40").getTime();
						long afterb = dfs.parse("07:40").getTime();
						
				String morn_work_time =getPara("checkingFormMap.morn_work_time").toString();
				
				 long one = dfs.parse(morn_work_time).getTime();
				String morn_leave_time = getPara("checkingFormMap.morn_leave_time").toString();
				 long two = dfs.parse(morn_leave_time).getTime();
				String after_work_time = getPara("checkingFormMap.after_work_time").toString();
				long three = dfs.parse(after_work_time).getTime();
				String after_leave_time = getPara("checkingFormMap.after_leave_time").toString();
				long four = dfs.parse(after_leave_time).getTime();
				int couts=0;
				if(one>mornjudges||one>mornjudge){
					checkingFormMap.put("decision", 2);
					couts++;
				}
				if(couts<1&&two<forenoon||two<forenoons){
					checkingFormMap.put("decision", 2);
					couts++;
				}
				if(couts<1&&three<mornjudgec||two<mornjudgecs){
					checkingFormMap.put("decision", 2);
					couts++;
				}
				if(couts<1&&four<aftera||two<afterb){
					checkingFormMap.put("decision", 2);
				}
				checkingMapper.editEntity(checkingFormMap);

				

	             
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return "false";
			}
			return "success";
		}
		

		
		
		
}
